1
|
|
|
/*eslint max-len: ["error", { "code": 150 }]*/ |
2
|
|
|
|
3
|
|
|
import React, { Component } from 'react'; |
4
|
|
|
import PropTypes from "prop-types"; |
5
|
|
|
import base from '../../../config/api.js'; |
6
|
|
|
let api = base.api(); |
7
|
|
|
|
8
|
|
|
class Edit extends Component { |
9
|
|
|
static propTypes = { |
10
|
|
|
match: PropTypes.object.isRequired, |
11
|
|
|
location: PropTypes.object.isRequired, |
12
|
|
|
history: PropTypes.object.isRequired |
13
|
|
|
}; |
14
|
|
|
constructor(props) { |
15
|
|
|
super(props); |
16
|
|
|
this.updateText = this.updateText.bind(this); |
17
|
|
|
this.registerSubmit = this.registerSubmit.bind(this); |
18
|
|
|
this.getReports = this.getReports.bind(this); |
19
|
|
|
this.showReport = this.showReport.bind(this); |
20
|
|
|
this.state = { |
21
|
|
|
current: { |
22
|
|
|
title: "", |
23
|
|
|
content: "" |
24
|
|
|
}, |
25
|
|
|
options: "", |
26
|
|
|
reports: "" |
27
|
|
|
}; |
28
|
|
|
} |
29
|
|
|
componentDidMount() { |
30
|
|
|
this.getReports(); |
31
|
|
|
} |
32
|
|
|
updateText(e) { |
33
|
|
|
this.setState({ |
34
|
|
|
current: { |
35
|
|
|
title: this.state.current.title, |
36
|
|
|
content: e.target.value |
37
|
|
|
}, |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
registerSubmit(event) { |
41
|
|
|
event.preventDefault(); |
42
|
|
|
const data = new FormData(event.target); |
43
|
|
|
|
44
|
|
|
let report = { |
45
|
|
|
"title": data.get('title'), |
46
|
|
|
"content": data.get('content') |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
fetch(api + "/reports/edit", { |
50
|
|
|
method: 'POST', |
51
|
|
|
body: JSON.stringify(report), |
52
|
|
|
headers: { |
53
|
|
|
'Content-Type': 'application/json' |
54
|
|
|
} |
55
|
|
|
}).then(this.props.history.push('/reports/week/1')); |
56
|
|
|
} |
57
|
|
|
getReports() { |
58
|
|
|
const that = this; |
59
|
|
|
|
60
|
|
|
let all = [1, 2, 3, 4, 5, 6, 10], |
61
|
|
|
options = [], |
62
|
|
|
reports = [], |
63
|
|
|
count = 0; |
64
|
|
|
|
65
|
|
|
all.forEach(function (report) { |
66
|
|
|
fetch(api + `/reports/week/${report}`) |
67
|
|
|
.then(res => res.json()) |
68
|
|
|
.then(function(res) { |
69
|
|
|
if (res.data.report.title !== "Report comming soon") { |
70
|
|
|
let title = res.data.report.title; |
71
|
|
|
|
72
|
|
|
options[report] = <option value={title}>{title}</option>; |
73
|
|
|
reports[report] = { |
74
|
|
|
title: title, |
75
|
|
|
content: res.data.report.content |
76
|
|
|
}; |
77
|
|
|
} |
78
|
|
|
if (all.length === count + 1) { |
79
|
|
|
that.setState({ |
80
|
|
|
options: options, |
81
|
|
|
reports: reports |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
count ++; |
85
|
|
|
}); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
showReport(e) { |
89
|
|
|
let current = this.state.reports.filter(function(report) { |
90
|
|
|
return report.title === e.target.value; |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
this.setState({ |
94
|
|
|
current: current[0] |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
render() { |
98
|
|
|
return ( |
99
|
|
|
<div className="form-wrapper"> |
100
|
|
|
<h1>Edit Report</h1> |
101
|
|
|
<form action="/reports/week/1" method="post" className="form-register" onSubmit={this.registerSubmit}> |
102
|
|
|
<label className="form-label">Title |
103
|
|
|
<select className="form-input" name="title" onChange={this.showReport} required> |
104
|
|
|
<option disabled>Select report</option> |
105
|
|
|
{this.state.options} |
106
|
|
|
</select> |
107
|
|
|
</label> |
108
|
|
|
|
109
|
|
|
<label className="form-label">Content |
110
|
|
|
<textarea |
111
|
|
|
className="form-input textarea" |
112
|
|
|
type="text" name="content" |
113
|
|
|
onChange={this.updateText} |
114
|
|
|
value={this.state.current.content} |
115
|
|
|
required |
116
|
|
|
placeholder="Select the report to edit"> |
117
|
|
|
</textarea> |
118
|
|
|
</label> |
119
|
|
|
|
120
|
|
|
<label className="form-label check-label"> |
121
|
|
|
<input className="check-input" type="checkbox" name="finished" required /> |
122
|
|
|
Are you finished? |
123
|
|
|
</label><br /> |
124
|
|
|
|
125
|
|
|
<input className="button form-button center" type="submit" name="edit" value="Save" /> |
126
|
|
|
</form> |
127
|
|
|
</div> |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
export default Edit; |
133
|
|
|
|